package com.tongji.housediy.view
{
	import com.tongji.housediy.events.commonEvent.ChangeData3D;
	import com.tongji.housediy.events.commonEvent.ExchangeDataEvent;
	import com.tongji.housediy.events.commonEvent.GridContainerEvent;
	import com.tongji.housediy.events.commonEvent.MoveCamera3D;
	import com.tongji.housediy.events.commonEvent.ResetRadioGroupEvent;
	import com.tongji.housediy.events.commonEvent.SetCompStateEvent;
	import com.tongji.housediy.events.commonEvent.ShowComplementInforEvent;
	import com.tongji.housediy.events.commonEvent.ShowHideTitleWindowEvent;
	import com.tongji.housediy.events.TypeXMLAdminEvent;
	import com.tongji.housediy.events.UpdateRatioCompEvent;
	import com.tongji.housediy.events.UpdateViewControlEvent;
	import com.tongji.housediy.model.CompEnableStateModel;
	import com.tongji.housediy.model.RightCompStateModel;
	import com.tongji.housediy.model.RightListStateModel;
	import com.tongji.housediy.model.ViewControlModel;
	
	import flash.display.Stage;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.utils.setTimeout;
	
	import mx.controls.ToolTip;
	import mx.core.UIComponent;
	import mx.managers.ToolTipManager;
	import mx.states.AddChild;
	import mx.styles.StyleManager;
	
	import org.robotlegs.mvcs.Mediator;
	import com.tongji.housediy.utils.ShowCustomToolTip;
	
	/**
	 * 
	 * @author chenchao
	 * 
	 */
	public class ViewChangeMediator extends Mediator
	{
		[Inject]
		public var view:ViewChangeComp;
		
		[Inject]
		public var stateModel:RightCompStateModel;
		
		[Inject]
		public var indexModel:RightListStateModel;
		
		[Inject]
		public var enableModel:CompEnableStateModel;
		
		[Inject]
		public var viewControlModel:ViewControlModel;
		
		private var selectedComp:UIComponent;
		
		override public function onRegister():void
		{
			selectedComp = view.flatButton;
			
//			StyleManager.getStyleDeclaration("ToolTip").setStyle("backgroundColor","#33CC99");
			this.addContextListener(ShowComplementInforEvent.SHOW_COMPLEMENT_INFOR, showComplementInfor, ShowComplementInforEvent)
			view.flatButton.addEventListener(MouseEvent.CLICK, flatButtonClickHandler);
			view.threeDButton.addEventListener(MouseEvent.CLICK, threeDButtonClickHandler);
		}
				
		private function showComplementInfor(event:ShowComplementInforEvent):void
		{
			var showCustomToolTip:ShowCustomToolTip = new ShowCustomToolTip(view.threeDButton, "查看三维");
		}
		
		private function flatButtonClickHandler(event:MouseEvent):void
		{
			if (selectedComp == view.threeDButton)
			{
				selectedComp = view.flatButton;
				
				view.flatButton.selected = true;
				view.threeDButton.selected = false;
			
				dispatch(new UpdateViewControlEvent(UpdateViewControlEvent.TWO_DIMENSION_VIEW));
				dispatch(new ExchangeDataEvent(ExchangeDataEvent.RESET_BORDER_CHILD));
				dispatch(new ChangeData3D(ChangeData3D.REMOVE_DATA));
				
				// 还原 网格 大小和滚动条位置
				//dispatch(new GridContainerEvent(GridContainerEvent.RETRIEVE_GRID_CONTAINER));
				
				// 还原 标尺组件 大小
				dispatch(new UpdateRatioCompEvent(UpdateRatioCompEvent.UPATE_RATIO_COMP, 4));
				
				// 还原 3D 按钮的 enable 状态
				dispatch(new SetCompStateEvent(SetCompStateEvent.SET_COMP_NORMAL_STATE));
				
				// 设置 RightControlComp state
				stateModel.state = "State1";
				
				// 设置 Layer4Comp selectedIndex
				indexModel.stateIndex = 0; 
				
				enableModel.isRightTabBtnEnable = true;
				
				dispatch(new ResetRadioGroupEvent(ResetRadioGroupEvent.RESET_WALL_RADIO));
				dispatch(new GridContainerEvent(GridContainerEvent.UPDATE_GRID_SCALE, viewControlModel.dimensionValue - viewControlModel.tempDimensionValue));
				viewControlModel.tempDimensionValue = viewControlModel.dimensionValue;
			}
		}
		
		private function threeDButtonClickHandler(event:MouseEvent):void
		{
			if (selectedComp == view.flatButton)
			{
				selectedComp = view.threeDButton;
				
				view.flatButton.selected = false;
				view.threeDButton.selected = true;
				
				dispatch(new UpdateViewControlEvent(UpdateViewControlEvent.THREE_DIMENSION_VIEW));
				dispatch(new ExchangeDataEvent(ExchangeDataEvent.UPDATE_EXCHANGE_MODEL));
				dispatch(new ShowHideTitleWindowEvent(ShowHideTitleWindowEvent.HIDE_TITLE_WINDOW));
				dispatch(new MoveCamera3D(MoveCamera3D.CAMERA_SELECT, viewControlModel.dimensionValue - 5));
				// 还原 网格 大小和滚动条位置
				//*******dispatch(new GridContainerEvent(GridContainerEvent.RETRIEVE_GRID_CONTAINER));
//				
//				// 设置 RightControlComp state
//				stateModel.state = "State3";
//				
//				// 设置 Layer4Comp selectedIndex
//				indexModel.stateIndex = 6;
//				
//				enableModel.isRightTabBtnEnable = false;
//				
				// 设置 RightControlComp state
				stateModel.state = "State1";
				
				// 设置 Layer4Comp selectedIndex
				indexModel.stateIndex = 0;
				
				enableModel.isRightTabBtnEnable = true;
				
				// 重置 3D 类型 List1 内容
				dispatch(new TypeXMLAdminEvent(TypeXMLAdminEvent.UPDATE_WALL_LEVEL1_XML, "wall"));
				
				dispatch(new ResetRadioGroupEvent(ResetRadioGroupEvent.RESET_TAB_RADIO));
				dispatch(new ResetRadioGroupEvent(ResetRadioGroupEvent.RESET_BUILDING_RADIO));
				viewControlModel.tempDimensionValue = viewControlModel.dimensionValue;
			}
		}
	}
}